package org.liveboardgames.agricola.control.delegates.action.processor
{
    import org.liveboardgames.agricola.control.delegates.helper.GameHelper;
    import org.liveboardgames.agricola.control.events.intern.ActionEvent;
    import org.liveboardgames.agricola.model.enums.ActionOutputEnum;
    import org.liveboardgames.agricola.model.enums.HouseTypeEnum;
    import org.liveboardgames.agricola.vo.action.Action;
    import org.liveboardgames.agricola.vo.farm.Farm;
    import org.liveboardgames.agricola.vo.farm.FarmElement;
    import org.liveboardgames.agricola.vo.farm.FarmTile;
    import org.liveboardgames.agricola.vo.player.Player;
    import org.liveboardgames.common.dictionary.collections.IMap;
    
    public class RenovationActionProcessor extends ActionProcessor
    {
        
        public function RenovationActionProcessor(action:Action)
        {
            super(action);
        }
        
        /** Assumes the check has already been done beforehand. */
        override public function execute():void
        {
            var player:Player = action.target as Player;
            var farm:Farm = player.farm;
            
            // Get the material into which to renovate
            var newMaterial:String = getRenovationMaterial(action);
            
            // Pay the renovation cost
            var cost:IMap = action.cost;
            GameHelper.pay(farm, cost);
            
            // Transform the rooms
            for each (var tile:FarmTile in farm.tiles)
            {
                var farmElement:String = tile.farmElement;
                if (FarmElement.isRoom(farmElement))
                {
                    tile.farmElement = FarmElement.getRoomFromMaterial(newMaterial);
                }
            }
            
            // Notify that the action has been completed
            this.dispatchEvent(new ActionEvent(ActionEvent.ACTION_COMPLETE));
        }
        
        private function getRenovationMaterial(action:Action):String
        {
            var newMaterial:String;
            
            var output:String = action.actionOutput;
            if (ActionOutputEnum.RENOVATION_TO_CLAY == output)
            {
                newMaterial = HouseTypeEnum.CLAY;
            }
            else if (ActionOutputEnum.RENOVATION_TO_STONE == output)
            {
                newMaterial = HouseTypeEnum.STONE;
            }
            return newMaterial;
        }
    }
}